<?php


namespace app\user\controller;
use app\common\controller\Base;
use app\common\lib\Util;
use app\common\model\Order as orders;
use app\common\model\OrderReview as orderReviews;
use EasyWeChat\Factory;
use think\Request;

class Order extends Base
{
    //订单生成
    public function addOrder(Request $request,orders $order){
        $no = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $orderData = [
            'order_no'=>$no,
            'from_user_id'=>$this->userId,
            'classify'=>$request->classify,
            'worth'=>$request->worth,
            'weight_money'=>$request->weight_money,
            'total_amount'=>$request->total_amount,
            'delivery_name'=>$request->delivery_name,
            'delivery_phone'=>$request->delivery_phone,
            'ship_name'=>$request->ship_name,
            'ship_phone'=>$request->ship_phone,
            'status'=>0,
            'ship_address'=>$request->send_big_address.$request->send_particular_address,
            'delivery_address'=>$request->put_big_address.$request->put_particular_address,
        ];
        $ins = $order->save($orderData);
        if (!$ins){
            return Util::apiResponse([],'失败',400);
        }
        return Util::apiResponse([],'成功',200);
    }


    //调起微信支付
    public function WeixinPrePay(Request $request,orders $order) {
        $data = $order->where(array('order_no'=>$request->order_no))->find();
        $options = config('payment.');//微信支付的配置文件
        $app = Factory::payment($options);//支付组件
        $order_data = $app->order->unify([
            'trade_type'       => 'APP', // JSAPI，NATIVE，APP...
            'body'             => $data['classify'],
            'detail'           => $data['classify'],
            'out_trade_no'     => $data['order_no'],
            'total_fee'        => $data['pay_amount'], // 单位：分
            'notify_url'       => 'http://me.weiq52.com/wxpay', //微信服务器返回支付结果的地址
        ]);
        if ($order_data['return_code']=='SUCCESS'&& $order_data['return_code']=='SUCCESS'){
            $code_url = $app->jssdk->appConfig($order_data['prepay_id']);
            return Util::apiResponse(['order'=>$code_url],'支付成功',200);
        }else{
            throw new \Exception("微信支付异常！请稍后再试",400);
        }
    }

    public function paySuccess(){


    }

//待支付
public function no_money(orders $order){
    $no_money_order = $order->where(array('status'=>0,'from_user_id'=>$this->userId))->field('create_time,send_address,put_address,total_amount')->select();
    return Util::apiResponse(['order'=>$no_money_order],'显示成功',200);
}

//用户待接单
public function stay_order(orders $order){
    $stay_order_order = $order->where(array('status'=>1,'from_user_id'=>$this->userId))->field('create_time,send_address,put_address,total_amount')->select();
    return Util::apiResponse(['order'=>$stay_order_order],'显示成功',200);
}

//跑男待取货
public function no_get_order(orders $order){
    $no_get_order = $order->where(array('status'=>1,'to_user_id'=>$this->userId))->field('create_time,send_address,put_address,total_amount')->select();
    return Util::apiResponse(['order'=>$no_get_order],'显示成功',200);
}

//进行中
public function have_order(orders $order){
    $have_order_order = $order->where(array('status'=>2,'from_user_id'=>$this->userId))->field('create_time,send_address,put_address,total_amount')->select();
    return Util::apiResponse(['order'=>$have_order_order],'显示成功',200);
}

//已完成
public function finish_order(orders $order){
    $finish_order_order = $order->where(array('status'=>3,'from_user_id'=>$this->userId))->field('create_time,send_address,put_address,total_amount')->select();
    return Util::apiResponse(['order'=>$finish_order_order],'显示成功',200);
}

//评价
public function appraise(Request $request,orderReviews $orderReview){
    $uploadPath = './upload/admin/headimg';
    if (!is_dir($uploadPath)) {
        mkdir($uploadPath, 0777,true);
    }
    $files = $request->file('image');
    $data = [
        'user_id'=>$this->userId,
        'order_id'=>$request->order_id,
        'rating'=>$request->rating,
        'review'=>$request->review,
    ];
    if ($files) {
        foreach($files as $file) {
            //移动文件
            $info = $file->move($uploadPath);
            if ($info) {
                // 获取上传信息
                $url = $uploadPath . $info->getSaveName();
                $data['avatar'] = $url;
            } else {
                return Util::apiResponse([], '上传图片错误！', -1);
            }
        }
    }
    $order_review_data = $orderReview->save($data);
    if ($order_review_data){
        return Util::apiResponse([], '成功', 1);
    } else {
        return Util::apiResponse([], '失败', -1);
    }
}

}